package com.lry.basic.redis;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class HashApply {

    private Jedis jedis;

    @BeforeAll
    public void before(){
        jedis = RedisPool.getJedis();
    }

    @AfterAll
    public void after(){
        RedisPool.release(jedis);
    }


    @Test
    public void TestCacheUser(){
        String key = "userInfo:1";
        Map<String,String> userInfo = new HashMap<>();
        userInfo.put("userId","1");
        userInfo.put("username","lry");
        userInfo.put("age","24");
        jedis.hset(key,userInfo);

        System.out.println(jedis.hgetAll(key));
        String cursor = "0";
        do{
            ScanResult<Map.Entry<String, String>> result = jedis.hscan(key,cursor);
            cursor = result.getStringCursor();
            System.out.println(cursor);
            System.out.println(result.isCompleteIteration());
            List<Map.Entry<String, String>> list =  result.getResult();
            for(Map.Entry entry:list){
                System.out.println(entry.getKey());
                System.out.println(entry.getValue());
            }
        }while (!cursor.equals("0"));

    }
}
